//
//  TYMBBleListView.m
//  TY_MotorRemoteControl
//
//  Created by 徐伟新 on 2023/11/16.
//  Copyright © 2023 zhongdaodianzi. All rights reserved.
//

#import "TYMBBleListView.h"
#import "TYMBBleListTableViewCell.h"

@interface TYMBBleListView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation TYMBBleListView

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.contentV.layer.cornerRadius = 20.0;
    self.contentV.layer.masksToBounds = YES;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"TYMBBleListTableViewCell" bundle:nil] forCellReuseIdentifier:@"TYMBBleListTableViewCell"];
    [self addNotifications];
    [BLE_MAN startScanAction];
    self.animalView.isStart = BLE_MAN.isScaning;

    
}

- (void)addNotifications {
    
    [Notification_center addObserver:self selector:@selector(notificationHandle:) name:BLE_SCAN_UPDATE_NOTIFICATION object:nil];
    [Notification_center addObserver:self selector:@selector(notificationHandle:) name:BLE_CONNECTED_SUCCESS_NOTIFICATION object:nil];
    [Notification_center addObserver:self selector:@selector(notificationHandle:) name:BLE_DISCONNECTED_FAIL_NOTIFICATION object:nil];
    [Notification_center addObserver:self selector:@selector(notificationHandle:) name:BLE_SCAN_STATUS_CHANGE object:nil];

    
    
}

- (void)notificationHandle:(NSNotification *)notification {
    NSString *name = notification.name;
    if ([name isEqualToString:BLE_SCAN_STATUS_CHANGE]) {
        self.animalView.isStart = BLE_MAN.isScaning;
    }else if ([name isEqualToString:BLE_CONNECTED_SUCCESS_NOTIFICATION]) {
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0/*延迟执行时间*/ * NSEC_PER_SEC));

        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [self cancleBtnClicked:self.cancleBtn];
            
        });
        
    }
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:BLE_MAN.peripheralArray];
    if (BLE_MAN.peripheral && BLE_MAN.peripheral.state == CBPeripheralStateConnected) {
        if (![self.dataArray containsObject:BLE_MAN.peripheral]) {
            [self.dataArray insertObject:BLE_MAN.peripheral atIndex:0];
        }
    }
    [self.tableView reloadData];
    
    
}


- (IBAction)cancleBtnClicked:(UIButton *)sender {
    [BLE_MAN stopScanAction];
    [self removeFromSuperview];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40.0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TYMBBleListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TYMBBleListTableViewCell" forIndexPath:indexPath];
    CBPeripheral *per = self.dataArray[indexPath.row];
    if (per.state == CBPeripheralStateConnected) {
        cell.isConnected = YES;
        cell.nameLab.textColor = UIColorFromHex(0xffffff);
    }else {
        cell.isConnected = NO;
        cell.nameLab.textColor = UIColorFromHex(0xafafaf);
    }
    cell.nameLab.text = per.name;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CBPeripheral *per = self.dataArray[indexPath.row];
    BLE_MAN.isHandControl = YES;
    [BLE_MAN connectBleWith:per];
    
}


@end
